/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss.component.dynproperty.entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.jinhe.tss.core.exception.BusinessException;
import com.jinhe.tss.core.persistence.IEntity;
import com.jinhe.tss.core.util.DateUtil;

/**
 * <p> PropertyEntry.java </p>
 * 
 * @author Jon.King 2008/04/14 10:18:09
 * 
 * 自定义属性值 （defId, entityId）允许对应多个值
 */
@Entity
@Table(name = "component_dyn_propertyEntry")
@SequenceGenerator(name = "dyn_entry_sequence", sequenceName = "dyn_entry_sequence", initialValue = 1000, allocationSize = 10)
public class PropertyEntry implements IEntity{
    //动态属性值类型
    @Transient public static final int VAL_VARCHER  = 1;
    @Transient public static final int VAL_DATE     = 2;
    @Transient public static final int VAL_NUMERIC  = 3;
    @Transient public static final int VAL_LONG     = 4;   //大字段值，java中对应是String型
    @Transient public static final int VAL_FILE     = 5;  //文件路径类型值
    @Transient public static final int VAL_PASSWORD = 6; //密码类型值
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "dyn_entry_sequence")
    private Long id;       // 序号
    private Long defId;    // 自定义属性ID
    private Long entityId; // 对应实体ID
    
    private String valVarchar;  // 字符串
    private Date   valDate;     // 日期 
    private Long   valNumeric;  // 数值 
    private String valLong;     // 大字段 
    private String valFile;     // 上传文件 
    private String valPassword; // 密码 

    /**
     * 根据类型判断返回值
     * @param type
     * @return
     */
    public Object getValue(int type) {
        switch (type) {
        case VAL_VARCHER:
            return getValVarchar();
        case VAL_DATE:
            return getValDate();
        case VAL_NUMERIC:
            return getValNumeric();
        case VAL_LONG:
            return getValLong();
        case VAL_FILE:
            return getValFile();
        case VAL_PASSWORD:
            return getValPassword();
        default:
            throw new BusinessException("不支持该类型的自定义数据");
        }
    }
    
    /**
     * 根据类型给对象相应类型的字段设置值。
     * @param type
     * @param value
     */
    public void setValue(int type, Object value){
        if(value == null || (value instanceof String && "".equals(value))){
            return;
        }
        switch(type){
        case VAL_VARCHER:
            this.setValVarchar((String)value);
            break;
        case VAL_DATE:
            if(value instanceof String)
                this.setValDate(DateUtil.parse((String)value));
            if(value instanceof Date)
                this.setValDate((Date)value);      
            break;
        case VAL_NUMERIC:
            if(value instanceof Long)
                this.setValNumeric((Long)value);
            else{
                try{
                    this.setValNumeric(Long.valueOf((String)value));
                }catch(Exception e){
                    throw new BusinessException("输入的值有误，数字类型的字段输入字符必须全部是数字");
                }
            }
            break;
        case VAL_LONG:
            this.setValLong((String)value);
            break;
        case VAL_FILE:
            this.setValFile((String)value);
            break;
        case VAL_PASSWORD:
            this.setValPassword((String)value);
            break;
        default:
            throw new BusinessException("不支持该类型的自定义数据");
        }
    }

    /**
     * 判断对象的各个类型字段的值是否都为空。
     * 
     * @return
     */
    public boolean isVauleNull() {
        return getValDate() == null && getValLong() == null
                && getValNumeric() == null && getValVarchar() == null
                && getValFile() == null && getValPassword() == null;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getDefId() {
        return defId;
    }

    public void setDefId(Long defId) {
        this.defId = defId;
    }

    public Long getEntityId() {
        return entityId;
    }

    public void setEntityId(Long entityId) {
        this.entityId = entityId;
    }

    public String getValVarchar() {
        return valVarchar;
    }

    public void setValVarchar(String valVarchar) {
        this.valVarchar = valVarchar;
    }

    public Date getValDate() {
        return valDate;
    }

    public void setValDate(Date valDate) {
        this.valDate = valDate;
    }

    public Long getValNumeric() {
        return valNumeric;
    }

    public void setValNumeric(Long valNumeric) {
        this.valNumeric = valNumeric;
    }

    public String getValLong() {
        return valLong;
    }

    public void setValLong(String valLong) {
        this.valLong = valLong;
    }

    public String getValFile() {
        return valFile;
    }

    public void setValFile(String valFile) {
        this.valFile = valFile;
    }

    public String getValPassword() {
        return valPassword;
    }

    public void setValPassword(String valPassword) {
        this.valPassword = valPassword;
    }
}
